﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace p2p.Misc.Base64
{
    public class Base64
    {
        /// <summary>
        ///     Decode a base64 string and return the result
        /// </summary>
        /// <param name="encodedData">Text to be decoded</param>
        /// <returns>Decoded text as a String</returns>
        public static string Decode(string encodedData)
        {
            // convert the given string into a array of bytes of none decoded data
            byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
            // converts the byte array to a unicode string
            string returnValue = System.Text.Encoding.ASCII.GetString(encodedDataAsBytes);
            // returns a string
            return returnValue;
        }

        /// <summary>
        ///     Encode a string using base64
        /// </summary>
        /// <param name="encodedData">Text to be encoded</param>
        /// <returns>endcoded text as a String</returns>
        public static string Encode(string toEncode)
        {
            // converts the given string to a byte array of unicode chars 
            byte[] toEncodeAsBytes = System.Text.Encoding.ASCII.GetBytes(toEncode);
            // encodes the byte array to a base64 string
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
            //returns the resulting string
            return returnValue;
        }
    }
}
